package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.wd_role;
import com.service.RoleService;

@Controller
@RequestMapping("/Role")
public class RoleAction {
	@Autowired
	private RoleService roleservice;
	
	@RequestMapping("/RoleManager.do")
	public String RoleManager(Model model){
		List<wd_role> list = roleservice.selectList();
		model.addAttribute("list", list);
		return "RoleManager";
	}
	@RequestMapping("/RoleAddForward.do")
	public String RoleAddForward(){
		return "RoleAddForward";
	}
	@RequestMapping("/RoleAdd.do")
	public String RoleAdd(wd_role record){
		System.out.println(record.getRoleName());
		int i = roleservice.insert(record);
		if(i!=0){
			return "redirect:RoleManager.do";
		}
		return "Error";
	}
	@RequestMapping("/RoleUpdateForward.do")
	public String RoleUpdate(Integer roleId,Model model){
		wd_role bean = roleservice.selectByPrimaryKey(roleId);
		model.addAttribute("bean", bean);
		if(bean!=null){
			return "RoleUpdateForward";
		}
		return "Error";
	}
	@RequestMapping("/RoleUpdate.do")
	public String RoleUpdate(wd_role record){
		System.out.println(record.getRoleName());
		int i = roleservice.updateByPrimaryKey(record);
		if(i!=0){
			return "redirect:RoleManager.do";
		}
		return "Error";
	}
	@RequestMapping("/RoleDelete.do")
	public String RoleDelete(Integer roleId){
		int i = roleservice.deleteByPrimaryKey(roleId);
		if(i!=0){
			return "redirect:RoleManager.do";
		}
		return "Error";
	}
	@RequestMapping("/RoleTest.do")
	public void RoleTest(String a,HttpServletResponse response) throws IOException{
		System.out.println(a);
		List<wd_role> list = roleservice.selectList();
		PrintWriter out = response.getWriter();
		for(wd_role bean:list){
			if(a.equals(bean.getRoleName())){
				out.print(1);
			}else {
				out.print(0);
			}
		}
	}
	
	@RequestMapping("UserManager.do")
	public String AuthorizationManager(){
		return "UserManager";
	}
	
	@RequestMapping("DataAuthorizedManager.do")
	public String DataAuthorizedManager(){
		return "DataAuthorizedManager";
	}
	
	@RequestMapping("ChangeRoleManager.do")
	public String ChangeRoleManager(){
		return "ChangeRoleManager";
	}
}
